import string
from datetime import datetime, timedelta
import time
import platform
import os
import json
from kds_util.user_logbook import user_log as logger


def get_breed_from_contract(code):
    """根据合约解析得到品种"""
    return code.rstrip(string.digits)


def fetch_digit(data) -> int:
    return int("".join(list(filter(str.isdigit, data))))


def wait_next_minute(extra_secs=0):
    """等待直到下一个整分钟
    """
    t = datetime.now()
    t2 = t.replace(second=0, microsecond=0) + timedelta(minutes=1)
    dt = t2 - t
    secs = dt.seconds + dt.microseconds / 1e6 + extra_secs
    logger.info("***********************************************... will_wait_secs:[{}] ...".format(secs))

    time.sleep(secs)


def is_forbid_opt_position(bk_item):
    """仓位操作已经被禁止"""
    flag = ((bk_item.trade_type == "OpenOptPosition") or (bk_item.trade_type == "CloseOptPosition"))
    return flag


def is_forbid_opt_open_or_close(bk_item):
    """
    禁止开仓或平仓
    """
    flag = (bk_item.trade_type == "OpenOptPosition") or (bk_item.trade_type == "CloseOptPosition")
    return flag


def is_forbid_opt_open_position(bk_item):
    """
    禁止开仓操作
    """
    flag = (bk_item.trade_type == "OpenOptPosition")
    return flag


def is_difference_side(l6_side, l9_side, spec_side):
    """
    判断方向是否不一致，不一致返回True,否则返回False
    0表示与任何都一致，比如0与-1比，表示相一致
    :param l6_side:
    :param l9_side:
    :param spec_side:
    :return:
    """
    a = l6_side * l9_side
    b = l9_side * spec_side
    c = l6_side * spec_side
    return a < 0 or b < 0 or c < 0
